/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.interaction.mouse.client;

import java.util.List;

import org.soupframework.gwt.designerpanel.client.designer.impl.AbsolutePanelViewDesigner;
import org.soupframework.gwt.designerpanel.client.interaction.InteractionHandlerManager;

import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.event.shared.EventHandler;

public class AbsolutePanelMouseInteractionHandler<T, A extends AbsolutePanelViewDesigner<T>> extends ContainerMouseDnDInteractionHandler<T, A> {

	public AbsolutePanelMouseInteractionHandler(InteractionHandlerManager<T> manager, A designer) {
		super(manager, designer);
	}

	@Override
	protected boolean drag(MouseEvent<? extends EventHandler> event, boolean forceMove, boolean blockGridAlignment){
		boolean ret = super.drag(event, forceMove, blockGridAlignment);

		int deltaX = mouseX - lastHandledMouseX;
		int deltaY = mouseY - lastHandledMouseY;
		if(forceMove || deltaX != 0 || deltaY != 0){
			List<T> selectedObjects = this.getDesigner().getSelectedObjects(false);
			if(selectedObjects.size() > 0){
				this.getDesigner().moveObjectsPartial("" + this.mouseDownEventCount, selectedObjects, deltaX, deltaY, blockGridAlignment);
				return true;
			}
		}
		return ret;
	}

	@Override
	protected void addObjectInt(T newObject, int relativeMouseX, int relativeMouseY) {
		this.getDesigner().addObject(newObject, relativeMouseX, relativeMouseY);
	}
}
